<?php

namespace app\wx\controller;

use EasyWeChat\Message\Article;
use EasyWeChat\Message\News;
use GuzzleHttp\Client;
use think\Controller;
use think\Request;
use EasyWeChat\Foundation\Application;

//openId: 微信号相对于公众号的唯一标识
class ServerController extends Controller
{
    public function index()
    {


      /*  $options = [
            'debug' => true,
            'app_id' => 'wx6243094044e475c0',
            'secret' => '5f442f23f6caaba75ca2c9631b4875d7',
            'token' => 'easywechat',

            // 'aes_key' => null, // 可选

            'log' => [
                'level' => 'debug',
                'file' => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
            ],

            //...
        ];*/

        $app = new Application(config('wx'));

        $server=$app->server;
        $server->setMessageHandler(function ($message) {

            if ($message->Event=="CLICK"){
                //菜单的单击事件

                switch ($message->EventKey){
                    case "today":

                        $goods=[
                            [],
                            []
                        ];

                        $news=[];
                        foreach ($goods as $good){

                            $new = new News([
                                'title'       => $good['goods_name'],
                                'description' => '...',
                                'url'         => "http://www.baidu.com",
                                'image'       => $good['img'],
                                // ...
                            ]);
                            $news[]=$new;
                        }

                        return $news;



                        return "今日神共";
                        break;
                    case "tel":
                        return "地址：互联网学院\nTel:13854354643";
                        break;
                }
               // return $message->EventKey;

            }

            if ($message->Content=="美女"){

                return "你要的是美女排行";
            }

            if ($message->Content=="天气预报"){
                return "天气";
            }


            return $message->FromUserName."我不知道你在说啥？";
        });

        $response = $app->server->serve();

// 将响应输出
        $response->send(); // Laravel 里请使用：return $response;
    }

    public function call(){
        $app = new Application(config('wx'));
        $oauth = $app->oauth;

// 获取 OAuth 授权结果用户信息
        $user = $oauth->user();



       // halt($user);

       // $_SESSION['wechat_user'] = $user->toArray();
        //把用户信息存到session中
        session("wechat_user",$user->toArray());

        //$targetUrl = empty($_SESSION['target_url']) ? '/' : $_SESSION['target_url'];
       // $targetUrl=empty(session("target_url"))?"/":session("target_url");
        //取出有没有目标地址
        $targetUrl=session("target_url")??"/";


        //header('location:'. $targetUrl); // 跳转到 user/profile
        return redirect($targetUrl);
    }

    public function getMenu(){
        $app = new Application(config('wx'));

        $menu = $app->menu;
        $all=$menu->all();
        var_dump($all);
    }

    public function setMenu(){
        $buttons = [
            [
                "type" => "click",//button  <button id="today song">今日</button>
                "name" => "今日歌曲",
                "key"  => "today"
            ],
            [
                "name"       => "菜单",
                "sub_button" => [
                    [
                        "type" => "view",//a标签  <a href="http://www.soso.com">搜索</a>
                        "name" => "搜索",
                        "url"  => "http://www.soso.com/"
                    ],
                    [
                        "type" => "view",
                        "name" => "视频",
                        "url"  => "http://v.qq.com/"
                    ],
                    [
                        "type" => "click",
                        "name" => "联系方式",
                        "key" => "tel"
                    ],
                ],
            ],
        ];
        $app = new Application(config('wx'));
        $menu = $app->menu;
        $menu->add($buttons);

    }
    public function test(){

        echo   \request()->url();

    }
}
